package com.wwj.common.utils;

import com.wwj.common.constant.CommonConstant;
import io.jsonwebtoken.Claims;
import lombok.extern.slf4j.Slf4j;

import java.util.Date;
@Slf4j
public class TokenCheck {
    /**
     * token失效验证
     */
    public static boolean checkToken(String token) {
        JWTUtils jwtUtils = new JWTUtils();
        jwtUtils.setSignature(CommonConstant.ADMIN_TOKEN);
        Claims claims = jwtUtils.parseToken(token);
        if (claims == null) {
            return false;
        }
        Date expiration = claims.getExpiration();
        return !jwtUtils.judgeTokenExpiration(expiration);
    }

    /**
     * 获取token的userId
     */
    public static String getUserIdByToken(String token){
        JWTUtils jwtUtils = new JWTUtils();
        jwtUtils.setSignature(CommonConstant.ADMIN_TOKEN);
        Claims claims = jwtUtils.parseToken(token);
        return (String)claims.get("userId");
    }
}
